import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import { Server } from 'socket.io'
import { RedisService } from '../redis/RedisService'
import { RoomStateService } from '../room/RoomStateService'

@Injectable()
export class GiftService {
  constructor(
    private readonly io: Server,
    private readonly redisService: RedisService,
    private readonly roomStateService: RoomStateService,
    private readonly configService: ConfigService
  ) {}

  // 礼物相关业务逻辑
  async sendGift(roomId: string, giftData: any) {
    // 更新房间礼物统计
    await this.redisService.incr(`room:${roomId}:gifts:${giftData.type}`)

    // 广播礼物消息
    this.io.to(roomId).emit('gift-received', giftData)
  }

  async getGiftStats(roomId: string) {
    const giftTypes = await this.configService.get('gift.types')
    const stats = {}

    for (const type of giftTypes) {
      stats[type] = await this.redisService.get(`room:${roomId}:gifts:${type}`)
    }

    return stats
  }
}
